package com.uniknow.app.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.uniknow.app.R;
import com.uniknow.app.bin.WixAccountBean;
import com.uniknow.app.manager.UserManager;
import com.uniknow.app.operate.BaseListener;
import com.uniknow.app.operate.CommonOperate;
import com.uniknow.app.operate.impl.CommonOperateImpl;
import com.uniknow.app.util.StringUtils;
import com.uniknow.app.util.UIHelper;
import com.uniknow.app.widget.LoadingDialog;

public class DialogAddWixAccount extends Dialog {
	public static final int DIALOG_CANCEL = 0;
	public static final int DIALOG_CONFIRM = 1;
	private Context mContext;
	private CommonOperate operate;
	private WixAccountBean wixBean;
	private LoadingDialog loding;
	
	private EditText textDsc,textAppid,textAppkey,textToken,textUrl;
	private Button submitBtn,cancleBtn;

	public DialogAddWixAccount(Context context) {
		super(context,R.style.progressdialogStyle);
		setContentView(R.layout.dialog_add_wixaccount);
		Window window = getWindow();
		window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
		mContext = this.getContext();
		operate = CommonOperateImpl.getOperate(mContext);
		wixBean = new WixAccountBean();
		loding = new LoadingDialog(mContext);
		this.setCanceledOnTouchOutside(false);
		initView();
	}
	
	public void initView(){
		textDsc = (EditText) findViewById(R.id.dialog_add_wix_dsc);
		textAppid = (EditText) findViewById(R.id.dialog_add_wix_appid);
		textAppkey = (EditText) findViewById(R.id.dialog_add_wix_appkey);
		textToken = (EditText) findViewById(R.id.dialog_add_wix_apptoken);
		textUrl = (EditText) findViewById(R.id.dialog_add_wix_appurl);
		submitBtn = (Button) findViewById(R.id.dialog_add_wix_submit);
		cancleBtn = (Button) findViewById(R.id.dialog_add_wix_cancle);
		
		cancleBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				DialogAddWixAccount.this.cancel();
			}
		});
		submitBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				add();
			}
		});
	}
	
	public void add(){
		if(!checkData())return;
		loding.show();
		operate.addWixAccount(UserManager.userBean, wixBean, new BaseListener() {
			
			@Override
			public void onSuccess(Object o) {
				loding.cancel();
				DialogAddWixAccount.this.cancel();
				clearData();
				UIHelper.show(mContext, "添加成功！");
			}
			
			@Override
			public void onError(String e) {
				loding.cancel();
				UIHelper.show(mContext, e);
			}
		});
	}
	
	public boolean checkData(){
		if(StringUtils.isEmpty(textDsc)){
			UIHelper.show(mContext, "请填写公众号描述");
			return false;
		}
		wixBean.setDsc(textDsc.getText().toString().trim());
		if(StringUtils.isEmpty(textAppid)){
			UIHelper.show(mContext, "请填写Appid");
			return false;
		}
		wixBean.setAppid(textAppid.getText().toString().trim());
		if(StringUtils.isEmpty(textAppkey)){
			UIHelper.show(mContext, "请填写Appkey");
			return false;
		}
		wixBean.setAppkey(textAppkey.getText().toString().trim());
		if(StringUtils.isEmpty(textToken)){
			UIHelper.show(mContext, "请填写AppToken");
			return false;
		}
		wixBean.setApptoken(textToken.getText().toString().trim());
		if(StringUtils.isEmpty(textUrl)){
			UIHelper.show(mContext, "请填写AppUrl");
			return false;
		}
		wixBean.setAppurl(textUrl.getText().toString().trim());
		return true;
	}
	
	public void clearData(){
		textDsc.setText("");
		textAppid.setText("");
		textAppkey.setText("");
		textToken.setText("");
		textUrl.setText("");
	}
	

}
